/*
 * libustream: User-space stackable streams
 * @(#) $Id: rot13.c 2 2009-02-20 09:52:11Z mo@ilaven.net $
 */

/*
 * Copyright (c) 2002, 2008 Mo McRoberts.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) of this software may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_usmodules.h"

/* This module implements stream head support for disk files */

static com_result_t __stdcall UStream_Rot13_QueryInterface(IUStreamNode *intf, com_riid_t iid, void **out);
static uint32_t __stdcall UStream_Rot13_AddRef(IUStreamNode *intf);
static uint32_t __stdcall UStream_Rot13_Release(IUStreamNode *intf);
static com_result_t __stdcall UStream_Rot13_Write(IUStreamNode *intf, IUStream *stream, const void *buf, size_t *buflen);
static com_result_t __stdcall UStream_Rot13_Read(IUStreamNode *intf, IUStream *stream, void *buf, size_t *buflen);
static com_result_t __stdcall UStream_Rot13_Pending(IUStreamNode *intf, IUStream *stream, size_t *out);
static com_result_t __stdcall UStream_Rot13_Accept(IUStreamNode *intf, IUStream *stream, IUStreamNode **out, int *fdout);

DECLARE_CLASS(UStream_Rot13)
{
	DECLARE_IMPLEMENTS(UStream_Rot13, IUStreamNode);
	uint32_t refcount;
	uint8_t buffer[64];
};

DEFINE_CLASS_INTERFACE(UStream_Rot13, IUStreamNode)
{
	UStream_Rot13_QueryInterface,
	UStream_Rot13_AddRef,
	UStream_Rot13_Release,
	UStream_Tail_GetChildNode,
	UStream_Rot13_Write,
	UStream_Rot13_Read,
	UStream_Tail_Pending,
	UStream_Tail_Accept
};

extern com_result_t
us__rot13_push(IUStream *stream, IUStreamNode **out)
{
	UStream_Rot13 *f;
	
	(void) stream;
	
	if(NULL == (f = (UStream_Rot13 *) calloc(1, sizeof(UStream_Rot13))))
	{
		return errno;
	}
	INITIALISE_ADDREF_INTERFACE_POINTER(UStream_Rot13, f, IUStreamNode);
	*out = GET_INTERFACE_POINTER(f, IUStreamNode);
	return COM_S_OK;
}

static com_result_t __stdcall
UStream_Rot13_QueryInterface(IUStreamNode *intf, com_riid_t iid, void **out)
{
	if(com_guid_equal(iid, &IID_IUnknown) || com_guid_equal(iid, &IID_IUStreamNode))
	{
		UStream_Rot13_AddRef(intf);
		*out = (void *) intf;
		return COM_S_OK;
	}
	return COM_E_NOINTERFACE;
}

static uint32_t __stdcall
UStream_Rot13_AddRef(IUStreamNode *intf)
{
	DECLARE_SELF(UStream_Rot13, IUStreamNode, intf);
	
	self->refcount++;
	return self->refcount;
}

static uint32_t __stdcall
UStream_Rot13_Release(IUStreamNode *intf)
{
	DECLARE_SELF(UStream_Rot13, IUStreamNode, intf);
	
	self->refcount--;
	if(0 == self->refcount)
	{
		free(self);
		return 0;
	}
	return self->refcount;
}

static com_result_t __stdcall
UStream_Rot13_Write(IUStreamNode *intf, IUStream *stream, const void *buf, size_t *buflen)
{
	ssize_t r;
	size_t l, c, rem;
	uint8_t *pbuf;
	uint8_t b, m;
	
	DECLARE_SELF(UStream_Rot13, IUStreamNode, intf);

	pbuf = (uint8_t *) buf;
	r = COM_S_OK;
	while(*buflen)
	{
		l = *buflen;
		if(l > sizeof(self->buffer))
		{
			l = sizeof(self->buffer);
		}
		memcpy(self->buffer, pbuf, l);
		pbuf += l;
		for(c = 0; c < l; c++)
		{
			b = self->buffer[c];
			m = b & 32;
			b &= ~m;
			self->buffer[c] = ((b >= 'A') && (b <= 'Z') ? ((b - 'A' + 13) % 26 + 'A') : b) | m;
		}
		rem = l;
		r = IUStream_Write(stream, self->buffer, &rem);
		*buflen -= (l - rem);
		if(rem)
		{
			break;
		}
	}
	return r;
}

static com_result_t __stdcall
UStream_Rot13_Read(IUStreamNode *intf, IUStream *stream, void *buf, size_t *buflen)
{
	ssize_t r;
	size_t l, c, rem;
	uint8_t *pbuf;
	uint8_t b, m;
	
	DECLARE_SELF(UStream_Rot13, IUStreamNode, intf);

	rem = *buflen;
	r = IUStream_Read(stream, self->buffer, &rem);
	if(COM_S_OK != r)
	{
		return r;
	}
	rem = *buflen - rem;
	*buflen -= rem;
	
	pbuf = (uint8_t *) buf;
	r = COM_S_OK;
	while(rem)
	{
		l = rem;
		if(l > sizeof(self->buffer))
		{
			l = sizeof(self->buffer);
		}
		pbuf += l;
		for(c = 0; c < l; c++)
		{
			b = self->buffer[c];
			m = b & 32;
			b &= ~m;
			self->buffer[c] = ((b >= 'A') && (b <= 'Z') ? ((b - 'A' + 13) % 26 + 'A') : b) | m;
		}
		memcpy(pbuf, self->buffer, l);
		rem -= l;
	}
	return r;
}
